package com.trinity.web.controller.order;

import com.github.binarywang.wxpay.exception.WxPayException;
import com.trinity.common.annotation.Log;
import com.trinity.common.core.controller.BaseController;
import com.trinity.common.core.domain.AjaxResult;
import com.trinity.common.enums.BusinessType;
import com.trinity.common.utils.SecurityUtils;
import com.trinity.orders.dto.AddOrderDto;
import com.trinity.orders.dto.PayRecordDto;
import com.trinity.orders.service.IXcOrdersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Api(value = "客户端订单支付接口", tags = "订单支付接口")
@Slf4j
@RestController
@RequestMapping("/orders")
public class ClientOrdersController extends BaseController {
    @Resource
    private IXcOrdersService xcOrdersService;

    @ApiOperation("小程序下单")
    @Log(title = "下单", businessType = BusinessType.INSERT)
    @RequestMapping(value = "/createOrder", method = {RequestMethod.POST})
    @ResponseBody
    public AjaxResult createOrder(@RequestBody AddOrderDto addOrderDto) {
        try {
            //获取用户id
            String userId = SecurityUtils.getUserId().toString();
            //调用service 完成插入订单信息 插入支付记录 生成支付二维码
            PayRecordDto order = xcOrdersService.AppcreateOrder(userId, addOrderDto);

            return success(order);
        } catch (WxPayException e) {
            return error(e.getCustomErrorMsg());
        }


    }
}
